package com.qf.agro.service.sys.impl;


import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.qf.agro.common.sys.ResultInfo;
import com.qf.agro.mapper.sys.SysUserMapper;
import com.qf.agro.pojo.sys.SysUser;
import com.qf.agro.service.sys.SysUserService;

import com.qf.agro.utils.JwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @company：千锋教育
 * @author：zhy
 * @date：2023/6/29
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public ResultInfo login(String username, String password) {
        //1.定义查询条件对象
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        //2.设置条件
        queryWrapper.eq(username != null, SysUser::getUsername, username);
        //3.使用条件查询
        SysUser sysUser = userMapper.selectOne(queryWrapper);
        //4.判断用户是否null
        if (sysUser == null) {
            return ResultInfo.error("用户名或者密码错误");
        }
        //5.校验密码
        //使用hutool的工具类SecureUtil对密码进行加密
        if (!StringUtils.equals(sysUser.getPassword(), SecureUtil.md5(password))) {
            return ResultInfo.error("用户名或者密码错误");
        }
        //6.签发token
        String token = jwtUtil.createJWT(sysUser.getId().toString(), JSON.toJSONString(sysUser), null);
        //6.把结果返回
        return ResultInfo.success(token);
    }

    @Override
    public Page<SysUser> page(int pageNum, int pageSize, String name) {
        Page<SysUser> page = new Page(pageNum, pageSize);
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(name != null, SysUser::getUsername, name);
        Page<SysUser> pageInfo = userMapper.selectPage(page, queryWrapper);
        return pageInfo;
    }

    @Override
    public boolean checkUsername(String username) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(username != null, SysUser::getUsername, username);
        List<SysUser> sysUsers = userMapper.selectList(queryWrapper);
        if (sysUsers.size() != 0) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void save(SysUser sysUser) {
        sysUser.setPassword(SecureUtil.md5("qf1234"));
        sysUser.setState(1);
        sysUser.setGroupId(1L);
        sysUser.setManagerId(1L);
        userMapper.insert(sysUser);
    }

    @Override
    public void update(SysUser sysUser) {

        userMapper.updateById(sysUser);
    }

    @Override
    public void disabled(Long id) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null, SysUser::getId, id);
        SysUser sysUser = userMapper.selectOne(queryWrapper);
        if (sysUser.getState() == 0) {
            sysUser.setState(1);
            userMapper.update(sysUser, queryWrapper);
        } else {
            sysUser.setState(0);
            userMapper.update(sysUser, queryWrapper);
        }
    }
}
